package com.xjh.utils.resultutils;



import com.xjh.utils.enumutils.CodeEnum;
import lombok.Data;

/**
 * @Auther: chenyaohua
 * @Date: 2020/9/13
 * @Description: 返回值设置
 */
@Data
public class ResponseObj {

    private int code;

    private String message;

    private boolean isOk;

    private Object data;

    private int total;

    private String token;

    public ResponseObj(){}

    public ResponseObj(int total) {
        this.total = total;
    }

    public ResponseObj(int code, String message, boolean isOk, Object data){
        this.code = CodeEnum.EXECUTE_SUCCESS.getCode();
        this.message = CodeEnum.EXECUTE_SUCCESS.getDesc();
        this.isOk = isOk;
        this.data = data;
    }

    private ResponseObj setSelf(int code,String message,boolean isOk, Object data){
        this.code = code;
        this.message = message;
        this.isOk = isOk;
        this.data = data;
        return this;
    }

    public ResponseObj okPage(PageResult<?> pageResult) {
        ResponseObj ok = ok(pageResult.getRecords());
        ok.setTotal(pageResult.getTotal().intValue());
        return ok;
    }

    public ResponseObj ok(Object data){
        return setSelf(CodeEnum.EXECUTE_SUCCESS.getCode(),CodeEnum.EXECUTE_SUCCESS.getDesc(),true, data);
    }

    public ResponseObj ok(){
        return setSelf(CodeEnum.EXECUTE_SUCCESS.getCode(),CodeEnum.EXECUTE_SUCCESS.getDesc(),true, null);
    }

    public ResponseObj error(String msg){
        return setSelf(CodeEnum.EXECUTE_ERROR.getCode(),msg,false, null);
    }

    public ResponseObj tokenInvalid(){
        return setSelf(CodeEnum.LOGIN_INVALID.getCode(),CodeEnum.LOGIN_INVALID.getDesc(),false, null);
    }

    public ResponseObj loginFail(){
        return setSelf(CodeEnum.LOGIN_FAIL.getCode(),CodeEnum.LOGIN_FAIL.getDesc(),false, null);
    }

    public ResponseObj alreadyLogin(){
        return setSelf(CodeEnum.ALREADY_LOGIN.getCode(),CodeEnum.ALREADY_LOGIN.getDesc(),false, null);
    }

    public ResponseObj loginParamsError(){
        return setSelf(CodeEnum.USERNAME_PASSWORD_NULL.getCode(),CodeEnum.USERNAME_PASSWORD_NULL.getDesc(),false, null);
    }

    public ResponseObj paramsError(){
        return setSelf(CodeEnum.PARAMS_ERROR.getCode(),CodeEnum.PARAMS_ERROR.getDesc(),false, null);
    }

    public ResponseObj importFail(){
        return setSelf(CodeEnum.IMPORT_FAIL.getCode(),CodeEnum.IMPORT_FAIL.getDesc(),false, null);
    }

    public ResponseObj alreadyExists(){
        return setSelf(CodeEnum.ALREADY_EXIST.getCode(),CodeEnum.ALREADY_EXIST.getDesc(),false, null);
    }
}
